package com.gin.kotlin.no_gay_alliance.fragment.replylist

import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gin.kotlin.no_gay_alliance.activity.ReplyListActivity
import com.gin.kotlin.no_gay_alliance.activity.ReplyListActivity.Companion.KEY_ARGUMENT
import com.gin.kotlin.no_gay_alliance.adapter.reply.ReplyListAdapter
import com.gin.kotlin.no_gay_alliance.argument.ReplyListArgument
import com.gin.kotlin.no_gay_alliance.databinding.FragmentReplyPageAbstractBinding
import com.gin.kotlin.no_gay_alliance.webview.BbsCodeWebView
import com.gin.nga.response.reply.ReplyInfo

/**
 * 一页回复
 */
class ReplyPageFragment : AbstractReplyPageFragment() {
    private lateinit var recyclerView: RecyclerView

    /**
     * 正文WebView 小池
     */
    private lateinit var bbsCodeWebViews: HashMap<Int, BbsCodeWebView?>
    private lateinit var adapter: ReplyListAdapter

    /**
     * 初始化渲染组件
     */
    override fun initRenderComponent(b: FragmentReplyPageAbstractBinding) {
        // 渲染
        this.adapter = ReplyListAdapter(requireContext(), handler!!, this, launcher)

        //  创建一个RecyclerView
        this.recyclerView = RecyclerView(requireContext()).also {
            it.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            it.layoutManager = LinearLayoutManager(requireContext())
        }
        b.swipeRefreshLayout.addView(this.recyclerView)
        this.recyclerView.adapter = adapter
        this.bbsCodeWebViews = hashMapOf()
    }

    /**
     * 获取指定位置的 WebView
     * @param index Int
     * @return ReplyContentWebView
     */
    fun obtainWebView(index: Int): BbsCodeWebView {
        return bbsCodeWebViews[index] ?: (requireActivity() as ReplyListActivity).bbsCodeWebViewPool.obtainWebView().also { bbsCodeWebViews[index] = it }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        val views = bbsCodeWebViews.values.filterNotNull()
        Log.i(TAG, "onDestroy: 释放 第 ${this.argument.page} 页 replyContentWebViews: ${views.size} 个")
        views.forEach {
            it.busy = false
            (it.parent as? ViewGroup)?.removeView(it)
            it.loadDataWithBaseURL("")
        }
    }

    /**
     * 请求成功，执行渲染
     * @param replies MutableList<ReplyInfo>?
     */
    override fun onSuccess(replies: MutableList<ReplyInfo>?) {
        replies?.also { adapter.replaceData(replies) }
    }

    /**
     * 请求错误时的回调，如清空或隐藏渲染组件
     */
    override fun onError() {
        // 清空数据
        this.adapter.replaceData(listOf())
    }

    override fun scrollToFloor(floorNumber: Int) {
        handler?.toastDevInProgress()
    }

    override fun scrollToReply(replyId: Long) {
        handler?.toastDevInProgress()
    }

    companion object {
        fun newInstance(argument: ReplyListArgument) = ReplyPageFragment().also { it.arguments = Bundle().also { b -> b.putParcelable(KEY_ARGUMENT, argument) } }
    }
}
